package com.kaifamiao.trace;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Logger;

@WebServlet( { "/cookie/receive" , "/receive/cookie" })
public class CookieReceiveServlet extends HttpServlet {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    @Override
    protected void service(HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        logger.info( "从请求头中获取cookie字段的值" );
        String cookieValue = request.getHeader( "cookie" );
        logger.info( "cookie: " + cookieValue );

        logger.info( "通过request.getCookies()方法获取cookie并将它们转换为Cookie对象" );
        Cookie[] cookies = request.getCookies();
        logger.info( Arrays.toString( cookies ) );

        response.getWriter().println( "<p>Received</p>" );

    }
}
